# -*- coding:utf-8 -*-
# author： jf


import requests
import re
from pymongo import *

client = MongoClient('mongodb://localhost:27017')
db = client.test
images = db.qiushibaike


def getWordImage():
    for i in range(1, 35):
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                                 'AppleWebKit/537.36 (KHTML, like Gecko) '
                                 'Chrome/63.0.3239.132 Safari/537.36'}

        url = 'https://www.qiushibaike.com/pic/' + 'page/' + str(i) + '/?s=5057012'
        response = requests.get(url, headers)
        html = response.text
        print(response.status_code)
        print(response.url)
    #    print(html)

        reg = re.compile(r'<div class="content">.*?<span>(.*?)</span>.*?<a href=".*?" '
                         r'target="_blank">.*?<img src="(.*?)" '
                         r'alt=".*?" />.*?</a>', re.S)
        info = re.findall(reg, html)
        print(info)
        for title, image1 in info:
            print(title, image1)
            name = image1.split('/')[-1]
            url1 = 'https:' + image1
            print(url)

            response = requests.get(url1)
            image = response.content
            with open(r'F:\python1\file\图片\糗事百科\%s' % name, 'wb') as f:
                f.write(image)

            images.insert({'name': title, '图片': url})

getWordImage()
